package com.bookstore.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * @author 未来
 */
@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {


    /**
     * 配置多个分组名注册多个Docket对象
     */
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
    }

    /**
     * 配置docket以配置Swagger具体参数
     *
     * @return
     */
    @Bean
    public Docket docket(Environment environment) {
        // 设置要显示swagger的环境
        Profiles of = Profiles.of("dev", "test");
        // 判断当前是否处于该环境
        // 通过 enable() 接收此参数判断是否要显示
        boolean flag = environment.acceptsProfiles(of);
        System.out.println("flag:" + flag);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                //配置分组名
                .groupName("hello")
                //是否开启swagger:flag
                .enable(false)
                .select()
                //设置扫描的包名
                .apis(RequestHandlerSelectors.basePackage("com.bookstore.Controller"))
                //扫描以books开头的路径：***，检查一个controller
//                .paths(PathSelectors.ant("/books/**"))
                .build();
    }

    //配置文档信息
    private ApiInfo apiInfo() {
        Contact contact = new Contact("万江", "http:www.baidu.com", "2134960009@qq.com");
        return new ApiInfo(
                // 标题
                "Swagger网络书店接口文档",
                // 描述
                "演示书店接口",
                // 版本
                "v2.0",
                // 组织链接
                "http://terms.service.url/组织链接",
                // 联系人信息
                contact,
                // 许可
                "Apach 2.0 许可",
                // 许可连接
                "许可链接",
                // 扩展
                new ArrayList<>()
        );
    }
}
